package top.lc951.myandroid.views.card;

import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

public class OverlayTransformerLan implements ViewPager.PageTransformer {
    public static final String TAG = OverlayTransformerLan.class.getSimpleName();
    public float mMinScale = 0.85f;
    public ViewPager mViewPager;
    public float mOffset = 40;
    public static final float DEFAULT_CENTER = 0.5f;

    public final int overlayCount;

    public OverlayTransformerLan(int overlayCount, float minScale) {
        this.overlayCount = overlayCount;
        mMinScale = minScale;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void transformPage(View page, float position) {
        Log.i(TAG, "position =" + position);
        if (mViewPager == null) {
            mViewPager = (ViewPager) page.getParent();
        }
        int current = mViewPager.getCurrentItem();
        Log.i(TAG, "current=" + current);
        int pageWidth = page.getWidth();
        Log.i(TAG, "pageWidth =" + pageWidth);

        int pageHeight = page.getHeight();
        Log.i(TAG, "pageHeight =" + pageHeight);

        page.setAlpha(0.5f);
        if (position <= 0) {
            page.setTranslationX(-position * page.getWidth()
                    + 0.85f * mOffset * position
                    + 1.6f * current * mOffset
            );
            float scaleFactor = (1 + position) * (1 - mMinScale) + mMinScale;
            page.setScaleX(scaleFactor);
            page.setScaleY(scaleFactor);

            page.setTranslationZ(position);
            page.setTranslationY((-position) * mOffset * 0.5f);

            page.setPivotX(-pageWidth / 3);
        } else if (position <= overlayCount - 1) {
            //移动X轴坐标，使得卡片在同一坐标
            page.setTranslationX(-position * page.getWidth()
                    + mOffset * position
                    + 1.6f * current * mOffset
            );
            float scaleFactor = (1 - position) * (1 - mMinScale) + mMinScale;
            page.setScaleX(scaleFactor);
            page.setScaleY(scaleFactor);

            page.setTranslationY((position) * mOffset * 0.5f);
            page.setTranslationZ(-position);
            page.setPivotX(pageWidth);
        } else {
            page.setAlpha(0f);
        }

    }

}
